package com.lonwin.inspectionmanagement.widget;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;

/**
 * Author: zhangjing
 * DATA:2018/11/21 下午8:47
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class RecylerPopwindow<T extends BaseQuickAdapter> extends PopupWindow{
    RecyclerView mRecyclerView;
    View rootview;
    private T adapter;
    private Context mContext;
    Window window;
    private ShowListener mShowListener;

    public void setShowListener(ShowListener showListener) {
        mShowListener = showListener;
    }

    public RecylerPopwindow(Context context) {
        this(context,null);
    }
    public RecylerPopwindow(T adapter,Context context) {
        this(context,null);
        this.adapter=adapter;
        mContext=context;
        if (mContext instanceof Activity){
            window=((Activity)mContext).getWindow();
        }
        initRecylerView(context);
    }

    public RecylerPopwindow(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecylerPopwindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(null);
        setAnimationStyle(R.style.mypopwindow_anim_style);
    }

    private void initRecylerView(Context context){
        rootview= LayoutInflater.from(context).inflate(R.layout.singlerecylerview,null,false);
        mRecyclerView =rootview.findViewById(R.id.rvcontent);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerView.setAdapter(adapter);
        setContentView(rootview);
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        if (mShowListener!=null){
            mShowListener.show();
        }
    }

    public interface ShowListener{
        void show();
    }
}
